package cn.csust.net2.manager.server.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.csust.net2.manager.server.dao.BaseDAO;
import cn.csust.net2.manager.server.dao.ParameterDAO;
import cn.csust.net2.manager.server.dao.StudentDAO;
import cn.csust.net2.manager.server.remote.BeanUtils;
import cn.csust.net2.manager.server.util.Util;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.Academy;
import cn.csust.net2.manager.shared.po.Major;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Parameter;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.SchoolRegisterService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.ServiceConstant;

import com.extjs.gxt.ui.client.data.BaseListLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
/**
 * 业务逻辑写在这个类中
 * @author Administrator
 *
 */
@Service(value = ServiceConstant.SCHOOL_REGISTER)
public class SchoolRegisterServiceImpl extends BaseServiceImpl  implements SchoolRegisterService{
	@Resource(name = Constant.DAO_NAME_PARAMETER)
	private ParameterDAO parameterDAO;
	@Resource(name = Constant.DAO_NAME_STUDENT)
	private StudentDAO studentDAO;
	/**
	 * 查找全部
	 */
	@Transactional
	@Override
	public <T extends PO>  BasePagingLoadResult<T> findAll(String cla,PagingLoadConfig config)throws BeyondAuthorityException {
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		Student student = config.get("student");
		StringBuffer s = new StringBuffer(" from "+cla+" c where 1=1 ");
		
		if(student!=null){
			System.out.println("加载表格的用户名为"+student.getName());
			s.append(" and c.user.userID = "+student.getUserID());
		}
		return dao.findAllByPaging(config,s.toString());
	}
	
	
	@Transactional
	@Override
	public <T extends PO> List<T> findAll(String cla, BaseListLoadConfig config)throws BeyondAuthorityException {
		// TODO Auto-generated method stub
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		Academy academy = config.get("academy");
		Major major = config.get("major");
		StringBuffer s = new StringBuffer(" from "+cla+" c where 1=1 ");
		if(academy!=null) {
			s.append(" and c.academy.academyID = " + academy.getAcademyID());
		} else if(major!=null) {
			s.append(" and c.major.majorID = " + major.getMajorID() + " ");
		}
		return dao.findAll(config, s.toString());
	}
	@Transactional
	@Override
	public boolean testRegisterAble() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Parameter entranceRegisterDate =parameterDAO.findUniqueHQL_T("from Parameter where paraName = 'entranceRegisterDate'");
		Parameter entranceRegisterDate_last =parameterDAO.findUniqueHQL_T("from Parameter where paraName = 'entranceRegisterDate_last'");
		int last = Integer.valueOf(entranceRegisterDate_last.getValue());
		try {
			Date s = df.parse(entranceRegisterDate.getValue());
			Date date = Util.judgeYearNow(s);
			if(date!=null){
				entranceRegisterDate.setValue(df.format(date));
				parameterDAO.save(entranceRegisterDate);
				s = df.parse(entranceRegisterDate.getValue());
			}
			return Util.juge(s, last);
			
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		return false;
	}


	public ParameterDAO getParameterDAO() {
		return parameterDAO;
	}


	public void setParameterDAO(ParameterDAO parameterDAO) {
		this.parameterDAO = parameterDAO;
	}


	public StudentDAO getStudentDAO() {
		return studentDAO;
	}


	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}
}
